package top.andoudou.generator.mybatis.config;

import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import javax.annotation.PostConstruct;

/** 
* @Description: 代码生成工具配置
* @Author: 安静思
* @Date: 2019\11\4
*/
@ConfigurationProperties("code")
@Data
public class CodeProperties {
    /**
     * 全局配置
     */
    @NestedConfigurationProperty
    GlobalConfig global;

    /**
     * 数据库配置
     */
    @NestedConfigurationProperty
    DataSourceConfig dataSource;

    /**
     * 生成策略
     */
    @NestedConfigurationProperty
    StrategyConfig strategy;

    /**
     * 包名
     */
    @NestedConfigurationProperty
    PackageConfig packages;

    /**
     * 模板配置
     */
    @NestedConfigurationProperty
    TemplateConfigExt template;

    /**
     * 定义配置
     */
    String  extClass = "top.andoudou.generator.mybatis.config.InjectionConfigExt";

    InjectionConfig injectionConfig;

    @PostConstruct
    void init() throws IllegalAccessException, InstantiationException, ClassNotFoundException {
       if( this.injectionConfig !=null){
           return;
       }

        Class cls  = Class.forName(extClass);
        if(!ContextSetting.class.isAssignableFrom(cls)){
            return;
        }
        ContextSetting setting = (ContextSetting) cls.newInstance();
        setting.init(this);

        this.injectionConfig = setting.getInjectionConfig();
    }

    public GlobalConfig getGlobal() {
        return global;
    }

}
